from flask import render_template, current_app, request, g
from flask.json import jsonify

from info import constants
from info.models import Goods, Category, Shop_cart
from info.utils.common import user_login_data
from info.utils.response_code import RET
from . import index_blu


# 首页
@index_blu.route('/', methods=["GET"])
@user_login_data
def index():
    """首页"""
    user = g.user

    try:
        goods_list = Goods.query.order_by(Goods.inventory.desc()).limit(10)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='商品错误')

    try:
        categories = Category.query.order_by(Category.c_id.desc()).all()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='数据错误')

    c1 = None
    c2 = None
    for category in categories:
        if category.c_name == '服装鞋包':
            c1 = category
        if category.c_name == '个护美妆':
            c2 = category

    try:
        goods_cate_1 = Goods.query.filter(Goods.category_id == c1.c_id).all()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='数据错误')

    try:
        goods_cate_2 = Goods.query.filter(Goods.category_id == c2.c_id).all()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='数据错误')

    list_goods1 = []
    list_goods2 = []
    for goods in goods_cate_1:
        list_goods1.append(goods.to_dict())

    for goods in goods_cate_2:
        list_goods2.append(goods.to_dict())
    goods_li_one = []
    goods_li_two = []
    for index, goods in enumerate(goods_list):
        if index <= 5:
            goods_li_one.append(goods.to_dict())
        if 5 <= index <= 10:
            goods_li_two.append(goods.to_dict())

    if user:
        try:
            shop_car = Shop_cart.query.filter(Shop_cart.c_user_id == user.u_id).first()
        except Exception as e:
            current_app.logger.error(e)
            return jsonify(errno=RET.DBERR, errmsg="购物车为空")
    else:
        shop_car = None
    try:
        categories = Category.query.all()
    except:
        return jsonify(errno=RET.DBERR, errmsg="数据异常")

    data = {
        "user_info": user.to_dict() if user else None,
        'goods_li_one': goods_li_one,
        'goods_li_two': goods_li_two,
        'list_goods1': list_goods1,
        'list_goods2': list_goods2,
        'shop_car': shop_car,
        "categories": categories,
    }

    return render_template('index/index.html', data=data)


@index_blu.route('/account')
@user_login_data
def account():
    user = g.user
    data = {
        'user_info': user
    }
    return render_template('index/zhanghuanquan.html', data=data)


# -------------------------------------------------------------------------------------------------------
# 绑定手机
@index_blu.route('/bdsj')
def bdsj():
    return render_template('index/bangdingshouji.html')


# 绑定邮箱
@index_blu.route('/bdyx')
def bdyx():
    return render_template('index/bangdingyouxiang.html')


# 成功绑定
@index_blu.route('/cgbd')
def cgbd():
    return render_template('index/chenggongbangding.html')


# 修改资料
@index_blu.route('/xgzl')
def xgzl():
    return render_template('index/xiugaiziliao.html')


# 详情页
@index_blu.route('/xqy')
def xqy():
    return render_template('index/xiangqingye.html')


# 我要评价
@index_blu.route('/wypj')
def wypj():
    return render_template('index/woyaopingjia.html')


# 我收藏的店铺
@index_blu.route('/wscddp')
def wscddp():
    return render_template('index/woshoucangdedianpu.html')


# 我的收藏
@index_blu.route('/wdsc')
def wdsc():
    return render_template('index/wodeshoucang.html')


# 我的购物车
@index_blu.route('/wdgwc')
def wdgwc():
    return render_template('index/wodegouwuche.html')


# 提交支付
@index_blu.route('/tjzf')
def tjzf():
    return render_template('index/tijiaozhifu.html')


# 填写核对购物信息
@index_blu.route('/txhdgwxx')
def txhdgwxx():
    return render_template('index/tianxieheduigouwuxinxi.html')


# 列表页
@index_blu.route('/lby')
def lby():
    return render_template('index/liebiaoye.html')


# 申请提现
@index_blu.route('/sqtx')
def sqtx():
    return render_template('index/shenqingtixian.html')


# 交易投诉
@index_blu.route('/jyts')
def jyts():
    return render_template('index/jiaoyitousu.html')


# 进入店铺
@index_blu.route('/jrdp')
def jrdp():
    return render_template('index/jinrudianpu.html')


# 进入店铺详情
@index_blu.route('/jrdpxq')
def jrdpxq():
    return render_template('index/jinrudianpuxiangqing.html')


@index_blu.route('/favicon.ico')
def favicon():
    """浏览器标签上显示的网站图标"""
    # current_app.send_static_file是返回静态文件的方法
    return current_app.send_static_file('favicon.ico')
